<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .box {
      position: absolute;
      top: 100px;
      left: 0;
      width: 100px;
      height: 100px;
      background-color: skyblue;
    }
  </style>
</head>
<body>
  <input type="button" value="开始" id="start">
  <input type="button" value="结束" id="end">
  <div class="box" id="box"></div>
  <script>
    var start = document.getElementById("start");
    var end = document.getElementById("end");
    var box = document.getElementById("box");
    var nowleft = 0;
    var timer;
    start.onclick = function(){
    //  设表先关方法
    clearInterval(timer);
    // 在定时器之前清除定时器
    timer = setInterval(function(){
            nowleft += 5;
            box.style.left = nowleft +"px";
        },50);
        end.onclick = function(){
        clearInterval(timer);
        }
    }



    // 点击开始box开始移动
    // 多次点击开始速度加块(相当于步长加长)
    // 多次点击开始后结束按钮按下不会停下来
    
    // var nowleft = 0;
    // start.onclick = function(){
    // var timer = setInterval(function(){
    //         nowleft += 10;
    //         box.style.left = nowleft +"px";
    //     },100);
    //     end.onclick = function(){
    //     clearInterval(timer);
    //     }
    // }
    

  </script>
</body>
</html>